package com.juyu.miliao.api.service;

import com.juyu.miliao.api.dao.mysql.UserVipDao;
import com.juyu.miliao.api.domain.UserVip;
import com.juyu.miliao.api.domain.Vip;
import com.juyu.miliao.api.domain.vo.VipVO;
import com.juyu.miliao.api.service.cache.VipCacheService;
import com.juyu.miliao.common.constant.AppPackageIdConstant;
import com.juyu.miliao.common.constant.EnumType;
import com.juyu.miliao.common.constant.VipType;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class VipService {
    @Resource
    private VipCacheService vipCacheService;
    @Autowired
    private UserVipDao userVipDao;


    public List<Vip> getVipList(String appPackageId) {
        if (AppPackageIdConstant.DEFAULT_APP_PACKAGE_ID.equals(appPackageId)) {
            return getVipListTimeOutCacheByType(EnumType.VIP_TYPE_HUAMI.state);
        } else if (AppPackageIdConstant.TONG_CHENG_MI_LIAO.equals(appPackageId)) {
            return getVipListTimeOutCacheByType(EnumType.VIP_TYPE_TCYL.state);
        } else if (AppPackageIdConstant.MO_MO_YUE_HUI.equals(appPackageId)) {
            return getVipListTimeOutCacheByType(EnumType.VIP_TYPE_MMJY.state);
        } else if (AppPackageIdConstant.LIAO_YOU_RE_XIAN.equals(appPackageId)) {
            return getVipListTimeOutCacheByType(EnumType.VIP_TYPE_HUAMI.state);
        } else if (AppPackageIdConstant.MI_BAN.equals(appPackageId)) {
            return getVipListTimeOutCacheByType(EnumType.VIP_TYPE_MIBAN.state);
        } else {
            return getVipListTimeOutCacheByType(EnumType.VIP_TYPE_HUAMI.state);
        }

    }

    public List<Vip> getPayVipList(String appPackageId) {
        if (AppPackageIdConstant.TONG_CHENG_MI_LIAO.equals(appPackageId)) {
            return getVipListTimeOutCacheByType(EnumType.VIP_TYPE_TCYL_POPUP.state);
        } else if (AppPackageIdConstant.MO_MO_YUE_HUI.equals(appPackageId)) {
            return getVipListTimeOutCacheByType(EnumType.VIP_TYPE_MMJY.state);
        }
        throw new ConsumeRuntimeException("此包不允许调用");
    }

    public List<Vip> getVipListByType(int type) {
        return getVipListTimeOutCacheByType(type);
    }

    private List<Vip> getVipListTimeOutCacheByType(int type) {
        List<Vip> vipList = vipCacheService.findVipList();
        List<Vip> resultList = new ArrayList<>();
        vipList.forEach(vip -> {
            if (vip.getVipType() == type)
                resultList.add(vip);
        });
        return resultList;
    }

    public Vip getVipById(int id) {
        List<Vip> vipList = vipCacheService.findVipList();
        if (vipList.size()==0)
            return null;
        Map<Integer, Vip> vipMap = vipList.stream()
                .collect(Collectors.toMap(Vip::getVipId, Function.identity()));
        return vipMap.get(id);
    }

    /**
     * 新vip列表
     * @param userId
     * @param type
     * @return
     */
    public Object getNewVipListByType(long userId, Integer type) {

        List<Vip> list = vipCacheService.findVipListByType(type);

        List<VipVO> commonResultList = new ArrayList<>();

        for(Vip vip:list){
            VipType vipType = VipType.COMMON_VIP_TYPE;
            if(vip.getVipClassification() == VipType.SUPER_VIP_TYPE.getCode()){
                vipType = VipType.SUPER_VIP_TYPE;
            }else if(vip.getVipClassification() == VipType.PLATINUM_VIP_TYPE.getCode()){
                vipType = VipType.PLATINUM_VIP_TYPE;
            }

            getShowVipVo(userId, commonResultList,vip,vipType);

        }


        return commonResultList;
    }

    /**
     * 设置vip展示信息
     * @param userId
     * @param commonResultList
     * @param vip
     * @param vipType
     */
    private void getShowVipVo(long userId, List<VipVO> commonResultList, Vip vip, VipType vipType) {
        UserVip userVip=userVipDao.getUserVipByUserIdAndVipType(userId, vipType.getCode());
        VipVO vipVO = new VipVO();
        int serverDay = vip.getValidTime();
        BeanUtils.copyProperties(vip,vipVO);

        if(userVip==null){
            //vip已经过期
            vipVO.setIsLoanOriginationFee(2);
            vipVO.setValidTime(0);
        }else{
            long day = ChronoUnit.DAYS.between(LocalDateTime.now(), userVip.getExpireTime());
            vipVO.setValidTime((int) day);
            vipVO.setIsLoanOriginationFee(1);
        }

        vipVO.setShowDay("/"+vip.getValidTime()+"天");

        vipVO.setVipClassification(vipType.getCode());
        vipVO.setServerDay(serverDay);
        vipVO.setPayGiveBeans(vip.getEverydayGiveBeans()*serverDay+vipVO.getPayGiveBeans());
        commonResultList.add(vipVO);
    }
}
